import Vue from 'vue'
import VueRouter from 'vue-router'
// 导入登录页面
import Login from '../components/Login.vue'
Vue.use(VueRouter)
// 导入首页
import Home from '../components/Home.vue'

// 导入子页面首页
import Welcome from '../components/Welcome.vue'

// 引入用户信息页面
import Users from '../components/user/Users.vue'

// 引入权限列表页面
import Rights from '../components/rights/Rights.vue'

// 引入角色列表
import Roles from '../components/rights/Roles.vue'


const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home', component: Home, redirect: '/welcome', children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: Users },
        { path: '/rights', component: Rights },
        { path: '/roles', component: Roles },

      ]
    },
  ]
})

// vue 路由守卫，进入页面的限制
router.beforeEach((to, from, next) => {
  // to:即将访问的页面
  // from：当前页面正要离开的路由
  // next：函数，表示通过
  // 若去的是login页面直接通过
  if (to.path == '/login') return next()
  // 否则查看token令牌是否正确
  const tokenStr = window.sessionStorage.getItem('token')
  //若没有token或token不正确，直接返回到登陆页面
  if (!tokenStr) return next('/login');
  // 否则token认证成功
  next();
})

export default router